<?php

use App\Enums\OpenShop\InvestmentPromotion\UserBaseInfoEnum;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateOpenShopUserBaseInfo extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('open_shop_user_base_info', function (Blueprint $table) {
            $table->id();

            $table->string('orderNo', 60)->default('')->comment('订单号');

            $table->string('mobile', 11)->default('')->comment('招商加盟注册手机号' );
            $table->string('remarkMobile1', 11)->default('')->comment('招商加盟备注手机号——1');
            $table->string('remarkMobile2', 11)->default('')->comment('招商加盟备注手机号--2');
            $table->string('email', 120)->default('')->comment('邮箱');

            $table->string('weChatNumber', 32)->default('')->comment('微信号');
            $table->tinyInteger('isMarried')->default('0')->comment('是否已婚 0:未婚 1:已婚');

            $table->string('provinceCode', 12)->default('')->comment('省份code');
            $table->string('cityCode', 12)->default('')->comment('市级code');
            $table->string('districtCode', 12)->default('')->comment('区域code');
            $table->string('addressPrefix', 120)->default('')->comment('地址前缀 - 省市区');
            $table->string('address', 120)->default('')->comment('详细地址信息');

            $table->string('postCode', 12)->default('')->comment('邮编');
            $table->tinyInteger('source')->default(0)->comment('来源渠道 - '.self::arrToStr(UserBaseInfoEnum::SOURCE));
            $table->tinyInteger('isSelfManaged')->default(0)->comment('是否亲自经营 - '.self::arrToStr(UserBaseInfoEnum::IS_SELF_MANAGED));
            $table->tinyInteger('joinReason')->default(0)->comment('加盟原因 - '.self::arrToStr(UserBaseInfoEnum::JOIN_REASON));
            $table->integer('shopBudget')->default(0)->comment('开店预算');
            $table->tinyInteger('investmentMode')->default(0)->comment('投资方式 - '.self::arrToStr(UserBaseInfoEnum::INVESTMENT_MODE));
            $table->tinyInteger('financeSource')->default(0)->comment('资金来源 - '.self::arrToStr(UserBaseInfoEnum::FINANCE_SOURCE));

            $table->string('shopProvinceCode', 12)->default('')->comment('门店省份code');
            $table->string('shopCityCode', 12)->default('')->comment('门店市级code');
            $table->string('shopDistrictCode', 12)->default('')->comment('门店区域code');
            $table->string('shopAddressPrefix', 120)->default('')->comment('门店地址前缀 - 省市区');
            $table->string('shopAddress', 120)->default('')->comment('门店地址');
            $table->string('shopName', 48)->default('')->comment('门店名称');

            $table->string('chooseShopProvinceCode', 12)->default('')->comment('门店选址省份code');
            $table->string('chooseShopCityCode', 12)->default('')->comment('门店选址市级code');
            $table->string('chooseShopDistrictCode', 12)->default('')->comment('门店选址区域code');
            $table->string('chooseShopAddressPrefix', 120)->default('')->comment('门店选址地址前缀 - 省市区');


            $table->tinyInteger('isSelfWithAddress')->default(0)->comment('是否自带选址 - '.self::arrToStr(UserBaseInfoEnum::IS_SELF_WITH_ADDRESS));
            $table->tinyInteger('chooseAddressReason')->default(0)->comment('选址原因 - '.self::arrToStr(UserBaseInfoEnum::CHOOSE_ADDRESS_REASON));

            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('createdByUid')->default(0)->comment('创建人');
            $table->unsignedInteger('updatedAt')->default(0)->comment('修改时间');
            $table->unsignedInteger('updatedByUid')->default(0)->comment('修改人');

            $table->index(['mobile'], 'idx_mobile');
            $table->unique(['orderNo'], 'idx_orderNo');
            $table->collation = 'utf8mb4_general_ci';
            $table->comment = '招商加盟 - 用户基础信息';
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('open_shop_user_base_info');
    }

    public static function arrToStr($data, $split = ":"): string
    {
        $str = '';
        foreach ($data as $k => $v){
            $str .= "{$k} {$split} {$v}";
        }
        return $str;
    }
}
